import axios from 'axios';

class HttpRequest {

    /* 构造函数 */
    constructor(option = {}) {
        this.defaults = {
            baseURL: ''
        }
        this.defaults = Object.assign(this.defaults, option)  //浅合并
    }

    /* axios 拦截器 */
    interceptors(axiosInstance) {
        // 请求拦截: 添加请求头
        axiosInstance.interceptors.request.use(
            config => {
                let token = localStorage.getItem('token');
                if (token) {
                    config.headers.authorization = `token ${token}`;  // 每次请求时，向后端检验token的合法性
                }
                // console.log(config);
                return config;
            },
            err => {
                return Promise.reject(err);
            }
        );

        // 响应拦截
        axiosInstance.interceptors.response.use(
            res => {
                const { data } = res;
                return data;
            },
            err => {
                return Promise.reject(err);
            }
        )
    }

    /* 请求 */
    request(option = {}) {
        option = Object.assign(this.defaults, option);
        // console.log(option);
        const axiosInstance = axios.create(option);  // axios子实例
        this.interceptors(axiosInstance);  // 添加拦截器
        return axiosInstance;
    }

}

const http = new HttpRequest({
    baseURL: '/api'
}).request();

/* 获取banner数据 */
async function getBanner() {
    return await http.get('/banner');
}

/* 上传图片: data.type = product | user | step */
async function upload(data) {
    return await http.post('/upload', data)
}



/* 注册账号:params.name\params.password */
async function register(data) {
    return await http.post('/user/create', data)
}
/* 登录 */
async function login(data) {
    return await http.post('/user/login', data)
}
/* 退出登录 */
async function login_out() {
    return await http.post('/user/login_out')
}
/* 获取用户信息 */
async function getUserInfo(data) {
    return await http.post('/user/info', data)
}
/* 修改用户信息 */
async function userEdit(data) {
    return await http.post('/user/edit', data)
}

/* 关注：指定收藏用户的id: followUserId */
async function toggleFollowing(data) {
    return await http.post('/user/following', data)
}
/* 获取指定用户的收藏菜单:userId */
async function getCollectionMenu(params) {
    return await http.get('/user/collection', { params })  // 第二个参数：get请求的配置项对象
}
/* 获取指定用户的关注 */
async function getFollowingList(params) {
    return await http.get('/user/following', { params })
}
/* 获取指定用户的粉丝 */
async function getFansList(params) {
    return await http.get('/user/fans', { params })
}



/* 获取所有属性分类 */
async function getProperty() {
    return await http.get('/menu/property')
}
/* 获取菜谱分类 */
async function getClassify() {
    return await http.get('/menu/classify')
}
/* 发布菜谱 */
async function publishRecipe(data) {
    return await http.post('/menu/publish', data)
}

/* 获取菜谱列表:
    null -> 获取所有菜谱
    params.userId -> 指定用户的菜单
    params.classify -> 指定菜单分类
    params.property -> 指定菜单属性
        params.property.craft -> 指定工艺
        params.property.favor -> 指定口味
        params.property.hard -> 指定难度
        params.property.people -> 指定人数
    params.page -> 指定页码
*/
async function getMenus(params) {
    return await http.get('/menu/query', { params })
}

/* 菜单详情：menuId */
async function menuInfo(params) {
    return await http.get('/menu/menuInfo', { params })
}

/* 收藏：指定收藏菜单的id */
async function toggleCollection(data) {
    return await http.post('/user/collection', data)
}

/* 获取菜谱评论信息: menuId */
async function getComments(params) {
    return await http.get('/menu/comment', { params })
}
/* 提交菜谱评论信息: menuId，commentText */
async function postComments(data) {
    return await http.post('/menu/comment', data)
}



export { getBanner, getProperty, getClassify, }

export { upload }  // 上传图片

export { register, login, login_out, getUserInfo, publishRecipe, }

export { userEdit, toggleFollowing, getCollectionMenu, getFollowingList, getFansList }

export { getMenus, menuInfo, toggleCollection, getComments, postComments }

